/**********************************************************************
* Filename: Jogo.java
* Created: 2012/04/23
* @author Fernando Brito e Abreu
**********************************************************************/
package org.quasar.copaPaises.businessLayer;

import java.util.Set;
import java.util.HashSet;
import java.util.SortedSet;
import java.util.TreeSet;

public class Jogo
{
	public static Set<Jogo> allInstances = new HashSet<Jogo>();
	
	private CalendarDate dia;
	private String hora;
	private Grupo grupo;
	private Estadio estadio;
	
	/**********************************************************************
	* Default constructor
	**********************************************************************/
	public Jogo()
	{
		allInstances.add(this);
	}
	
	/**********************************************************************
	* Parameterized constructor
	* @param dia the dia to initialize
	* @param hora the hora to initialize
	* @param grupo the grupo to initialize
	* @param estadio the estadio to initialize
	**********************************************************************/
	public Jogo(CalendarDate dia, String hora, Grupo grupo, Estadio estadio)
	{
		this.dia = dia;
		this.hora = hora;
		this.grupo = grupo;
		this.estadio = estadio;
		allInstances.add(this);
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the dia of the jogo
	**********************************************************************/
	public CalendarDate dia()
	{
		return dia;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param dia the dia to set
	**********************************************************************/
	public void setDia(CalendarDate dia)
	{
		this.dia = dia;
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the hora of the jogo
	**********************************************************************/
	public String hora()
	{
		return hora;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param hora the hora to set
	**********************************************************************/
	public void setHora(String hora)
	{
		this.hora = hora;
	}
	
	/**********************************************************************
	* ONE2MANY getter for Jogo[*] <-> Grupo[1]
	* @return the grupo of the jogo
	**********************************************************************/
	public Grupo grupo()
	{
		return grupo;
	}
	
	/**********************************************************************
	* ONE2MANY setter for Jogo[*] <-> Grupo[1]
	* @param grupo the grupo to set
	**********************************************************************/
	public void setGrupo(Grupo grupo)
	{
		this.grupo = grupo;
	}
	
	/**********************************************************************
	* ONE2MANY getter for Jogo[*] <-> Estadio[1]
	* @return the estadio of the jogo
	**********************************************************************/
	public Estadio estadio()
	{
		return estadio;
	}
	
	/**********************************************************************
	* ONE2MANY setter for Jogo[*] <-> Estadio[1]
	* @param estadio the estadio to set
	**********************************************************************/
	public void setEstadio(Estadio estadio)
	{
		this.estadio = estadio;
	}
	
	/**********************************************************************
	* MEMBER2ASSOCIATIVE getter for Jogo[*] <-> Equipa[2] ordered
	* @return the equipa of the jogo
	**********************************************************************/
	public SortedSet<Equipa> equipa()
	{
		SortedSet<Equipa> result = new TreeSet<Equipa>();
		for (Equipa x : Equipa.allInstances)
			if (x.jogo()  ==  this)
				result.add(x);
		return result;
	}
	
	/**********************************************************************
	* MEMBER2ASSOCIATIVE setter for Jogo[*] <-> Equipa[2] ordered
	* @param equipa the equipa to set
	**********************************************************************/
	public void setEquipa(SortedSet<Equipa> equipa)
	{
		for (Equipa x : equipa)
			x.setJogo(this);
	}
	
	/**********************************************************************
	* MEMBER2MEMBER getter for Jogo[*] <-> Pais[2] ordered
	* @return the seleccao of the jogo
	**********************************************************************/
	public SortedSet<Pais> seleccao()
	{
		SortedSet<Pais> result = new TreeSet<Pais>();
		for (Equipa x : Equipa.allInstances)
			if (x.jogo()  ==  this)
				result.add(x.seleccao());
		return result;
	}
	
	/**********************************************************************
	* MEMBER2MEMBER setter for Jogo[*] <-> Pais[2] ordered
	* @param seleccao the seleccao to set
	**********************************************************************/
	public void setSeleccao(SortedSet<Pais> seleccao)
	{
		for (Pais t : seleccao)
			for (Equipa x: Equipa.allInstances)
				if (x.jogo() == this)
					x.setSeleccao(t);
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	**********************************************************************/
	public boolean Organizador()
	{
		//	TODO
		//	return Campeonato.allInstances->collectNested($e : Campeonato | $e.participacao)->flatten->select(p : Participacao | p.organizador)->collectNested($e : Participacao | $e.participante)->collectNested($e : Pais | $e.nome)->includes(self.estadio.cidade.pais.nome)
		return true;
	}
	
	/* (non-Javadoc)
	* @see java.lang.Object#toString()
	*/
	@Override
	/**********************************************************************
	* Object serializer
	**********************************************************************/
	public String toString()
	{
		return "Jogo [dia=" + dia + ", hora=" + hora + ", grupo=" + grupo + ", estadio=" + estadio + "]\n";
	}
	
}
